<?php

$this->LoadHelper('MemberHelper');
$MemberHelper = new MemberHelper();

$this->LoadHelper('OrderHelper');
$OrderHelper = new OrderHelper();

$member_info = $MemberHelper->getMember($this->UserConfig['Id']);
$this->Assign('member_info', $member_info);
//if($member_info['u_dp_type'] == 'dp' && $member_info['u_scope'] == '单位级'){
//    $dp_code = 'GL03';
//}else{
//    $dp_code = $member_info['u_dpcode'];
//}
$dp_code = $member_info['u_dpcode'];

$this->LoadResurces('oldapi/class.carloan');

if (empty($this->Param['option'])) {
    $oldapi = new Carloan();
    $sign = $oldapi->sign(array('code' => $dp_code));
    $ajax = $this->Param['ajax'];
    $page = isset($this->Param['page']) ? $this->Param['page'] : 1;
    $rp = isset($this->Param['rp']) ? $this->Param['rp'] : PAGE_SIZE;
    if ($ajax) {
        $carloan = new Carloan();
        switch($member_info['u_scope']){
            case '用户级':
                $dpcode = '-1';
                break;
            case '部门级':
                $dpcode = $member_info['u_dpcode'];
                break;
            default :
                $dpcode = '';
        }
        $result = $carloan->getPayorder($dpcode, $page, $rp);
        $result = json_decode($result, TRUE);
        $json_data = array('total' => $result['total'], 'rows' => array(), 'pagetotal' => ceil($result['total'] / $rp));
        if (!empty($result['root'])){
            foreach ($result['root'] AS $rowNum => $row) {
                $row['pay_url'] = OLD_SITE_URL.'/PaymentsDetail/pay_load/id/'.$row['pd_id'].'/pay_item/'.$row['pd_items'].'?code='.$dp_code.'&sign='.$sign['sign'];
                $json_data['rows'][] = $row;
            }
        }
        echo json_encode($json_data);
    } else {        
        echo $this->GetView('order_paylist.php');
    }
} else {
    switch ($this->Param['option']){
        case 'dopay':
            $pd_id = empty($this->Param['pd_id']) ? ErrorMsg::Debug('参数错误') : $this->Param['pd_id'];
            $carloan = new Carloan();
            $result = $carloan->doPay($pd_id);
            $result = json_decode($result, TRUE);
            if($result['success']){
                ErrorMsg::Debug('支付成功', TRUE);
            }else{
                ErrorMsg::Debug('支付失败'.$result['msg']);
            }
            
            break;
        default :
    }
}